/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package central.server;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import share.Listener;

/**
 *
 * @author mire
 */
public class CentralServerListener extends Listener implements Runnable {

    private CentralServer centralServer;
    private Thread runningThread = null;

    public CentralServerListener(CentralServer centralServer, int operatorPort, ExecutorService threadPool) {
        super(operatorPort, threadPool);
        this.centralServer = centralServer;
    }

        public CentralServerListener(CentralServer centralServer, int operatorPort, String bindAddress, ExecutorService threadPool) {
        super(operatorPort, bindAddress, threadPool);
        this.centralServer = centralServer;
    }

    @Override
    public void run() {
        synchronized (this) {
            runningThread = Thread.currentThread();
        }

        openServerSocket();
        while (!isStoped()) {
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
                
            } catch (IOException ex) {
                Logger.getLogger(CentralServerListener.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            threadPool.execute(new CentralServerThread(clientSocket, centralServer));
        }
    }
}
